package com.story.modular.app.service.impl;

import com.story.common.exception.BizExceptionEnum;
import com.story.common.exception.BussinessException;
import com.story.common.persistence.dao.UserMapper;
import com.story.common.persistence.model.PayDetail;
import com.story.common.persistence.model.VipType;
import com.story.core.shiro.ShiroKit;
import com.story.modular.app.dao.PayDetailMapper;
import com.story.modular.app.dao.VipTypeMapper;
import com.story.modular.app.service.PayDetailService;
import com.story.modular.system.dao.UserMgrDao;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

/**
 * @author Pyx
 * @PACKAGE_NAME : com.story.modular.app.service.impl
 * @FileName: PayDetailImpl
 * @date 2020/4/26 10:34
 */
@Service
@Slf4j
public class PayDetailImpl implements PayDetailService {
    @Autowired
    private PayDetailMapper payDetailMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private VipTypeMapper vipTypeMapper;
    @Autowired
    private UserMgrDao userMgrDao;

    @Override
    public int save(PayDetail detail,Integer id) {
        if(id == null) {
             id = ShiroKit.getUser().getId();
        }
        detail.setUserId(id);
        detail.setUserName(userMapper.selectById(id).getName());
        VipType vipType = vipTypeMapper.selectByPrimaryKey(detail.getVipType());
        detail.setPayAmount(vipType.getPrice());
        detail.setRemark(vipType.getName());
        detail.setCreateTime(new Date());
        //判断用户余额是否够
        Map<String, Object> userDetail = userMgrDao.getUserDetail(id);
        String yunbeiBalance = userDetail.get("yunbei_balance").toString();
        if(new BigDecimal(yunbeiBalance).compareTo(vipType.getPrice())<0){
            throw new BussinessException(BizExceptionEnum.BALANCE_NOT_ENOUGH);
        }else{
            detail.setBalance(new BigDecimal(yunbeiBalance).subtract(vipType.getPrice()));
            userMgrDao.updateUserDto(vipType.getPrice(),id);
        }
        //判断用户是否是会员
        List<PayDetail> details = payDetailMapper.findByUserId(id);
        // 没记录 没充值过
        if (details.size() == 0) {
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.DAY_OF_YEAR, vipType.getDays());
            Date date = calendar.getTime();
            detail.setStartTime(new Date());
            detail.setEndTime(date);
        } else {
            Calendar calendar = new GregorianCalendar();
            PayDetail detail1 = details.get(0);
            Date endTime = detail1.getEndTime();
            calendar.setTime(endTime);
            if (endTime.getTime() < System.currentTimeMillis()) {
                //有记录但到期了
                calendar = Calendar.getInstance();
                calendar.add(Calendar.DAY_OF_YEAR, vipType.getDays());
                Date date = calendar.getTime();
                detail.setStartTime(new Date());
                detail.setEndTime(date);
            } else {
                calendar = Calendar.getInstance();
                Date date = detail1.getEndTime();
                calendar.setTime(date);
                calendar.add(Calendar.DAY_OF_YEAR, vipType.getDays());
                detail.setEndTime(calendar.getTime());
                detail.setStartTime(detail1.getStartTime());
            }
        }
        return payDetailMapper.insert(detail);
    }
}
